﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using System.Data;
using Cosmetic.DataEntity;
using Cosmetic;

namespace WebApp.Space.AccountInfo
{
    public partial class MemberAddressEdit : PageBase
    {
        UserManager m_UserManager = new UserManager();
        AddressManager addressmanger = new AddressManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser == null)
            {
                Page.RegisterClientScriptBlock("key", "<script>window.top.location = '/Login.aspx';</script>");
                return;
            }
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            DataSet ds = AddressManager.SelectByUserId(this.SignInUser.UserId.ToString());
            //this.LinkButton1.CommandArgument = ds.Tables[0].Rows[0]["AddressId"].ToString();
            if (ds.Tables[0].Rows.Count > 0)
            {
                string ReceiverName = ds.Tables[0].Rows[0]["name"].ToString();
                string Address = ds.Tables[0].Rows[0]["Address"].ToString();
                string Phone = ds.Tables[0].Rows[0]["Mobile"].ToString();
                if (!string.IsNullOrEmpty(ReceiverName))
                {
                    this.txtReceiverName.Text = ReceiverName;//收货人。
                }
                if (!string.IsNullOrEmpty(Address))
                {
                    this.txtAddress.Text = Address;//地    址
                }
                if (!string.IsNullOrEmpty(Phone))
                {
                    this.txtPhone.Text = Phone;
                }

                hfAddressId.Value = ds.Tables[0].Rows[0]["AddressId"].ToString();
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

            if (string.IsNullOrEmpty(hfAddressId.Value))
            {
                Address address = new Address();
                address.Mark = true;
                address.AddressId = new Guid();
                //theaddress.Consignee = this.DropDownList1.SelectedItem.Text + this.DropDownList2.SelectedItem.Text + this.DropDownList3.SelectedItem.Text;
                address.UserId = this.SignInUser.UserId;

                address.Mobile = this.txtPhone.Text;
                address.Address1 = this.txtAddress.Text;
                address.name = this.txtReceiverName.Text;
                address.Consignee = "";
                address.Province = "";
                address.City = "";
                address.Area = "";
                address.Phone = "";
                address.District = "";
                addressmanger.Insert(address);
                //WebGeneric.WebMessageBox("添加成功!", this.Page);

                ClientScript.RegisterStartupScript(Page.GetType(), "key", "alert('添加成功!');window.navigate('MemberAddress.aspx');", true);

            }
            else
            {
                Address theaddress = addressmanger.GetAddressById(new Guid(hfAddressId.Value));
                theaddress.Mark = true;
                //theaddress.Consignee = this.DropDownList1.SelectedItem.Text + this.DropDownList2.SelectedItem.Text + this.DropDownList3.SelectedItem.Text;
                theaddress.UserId = this.SignInUser.UserId;

                theaddress.Mobile = this.txtPhone.Text;
                theaddress.Address1 = this.txtAddress.Text;
                theaddress.name = this.txtReceiverName.Text;

                addressmanger.Update();
                //WebGeneric.WebMessageBox("修改成功!", this.Page);

                ClientScript.RegisterStartupScript(Page.GetType(), "key", "alert('添加成功!');window.navigate('MemberAddress.aspx');", true);

                //this.HiddenField1.Value = "1";//为0表示是”修改“。
                //Bind();
            }
        }
    }
}